<?php

/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property string $id
 * @property string $username
 * @property string $realname
 * @property string $password
 * @property string $birthday
 * @property integer $sex
 * @property string $qq
 * @property string $email
 * @property string $role
 * @property integer $status
 * @property string $reg_time
 * @property string $last_login_time
 * @property integer $score
 * @property integer $star
 * @property string $description
 */
class User extends CActiveRecord
{
	public $verifyCode;
	public $password_repeat;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username, password', 'required', 'on'=>array('login', 'register', 'create')),
			array('username, email', 'unique'),
			array('username, email', 'required', 'on'=>array('update')),
			array('email, password_repeat', 'required', 'on'=>array('register', 'create')),
			array('password', 'compare', 'on'=>array('register', 'create', 'changepwd')),
			array('email', 'email'),
			// array('birthday', 'type', 'type' => 'date', 'message' => '{attribute}: 请输入一个yyyy-mm-dd格式的日期。', 'dateFormat' => 'yyyy-MM-dd'),
			array('qq, sex, status, score, star', 'numerical', 'integerOnly'=>true),
			array('username', 'length', 'max'=>20),
			array('realname, role, reg_time, last_login_time', 'length', 'max'=>10),
			array('password', 'length', 'max'=>32),
			array('qq', 'length', 'max'=>50),
			array('email', 'length', 'max'=>255),
			array('birthday, description, password_repeat', 'safe'),
			array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements(), 'on'=>'register'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, username, realname, password, birthday, sex, qq, email, role, status, reg_time, last_login_time, score, star, description', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'username' => '作者名',
			'realname' => '真实姓名',
			'password' => '密码',
			'password_repeat'=>'重复密码',
			'birthday' => '出生日期',
			'sex' => '性别',
			'qq' => 'QQ号码',
			'email' => 'Email',
			'role' => '角色',
			'status' => '状态',
			'reg_time' => '注册于',
			'last_login_time' => '最后登录',
			'score' => '积分',
			'star' => '星级',
			'description' => '简介',
			'verifyCode' => '验证码',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('realname',$this->realname,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('birthday',$this->birthday,true);
		$criteria->compare('sex',$this->sex);
		$criteria->compare('qq',$this->qq,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('role',$this->role,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('reg_time',$this->reg_time,true);
		$criteria->compare('last_login_time',$this->last_login_time,true);
		$criteria->compare('score',$this->score);
		$criteria->compare('star',$this->star);
		$criteria->compare('description',$this->description,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * 用户status的文字表述
	 * @return string
	 */
	public function getStatusText()
	{
		$options = DefineBox::$userStatusArray;
		return isset($options[$this->status]) ? $options[$this->status] : "未知($this->status)";
	}

	/**
	 * 用户sex的文字表述
	 * @return string
	 */
	public function getSexText()
	{
		$options = DefineBox::$sexArray;
		return isset($options[$this->sex]) ? $options[$this->sex] : "未知";
	}
	
	/**
	 * 用户sex的文字表述
	 * @return string
	 */
	public function getRoleText()
	{
		$options = DefineBox::$userRoleArray;
		return isset($options[$this->role]) ? $options[$this->role] : "未知";
	}
	

}